#import "YQPIntegerPayView.h"

#import "YQPOrderInfoModel.h"
#import "YQIntegralOrderDetailsVC.h"
#import "YQOrderDetailsHeaderView.h"
#import "YQOrderDetailsBottonView.h"
#import "OrderEvaViewController.h"
#import "YQOrderRefundShowView.h"
#import "YQOrderRefundVC.h"
#import "YQOrderLogisticsWriteVC.h"
@interface YQIntegralOrderDetailsVC ()<YQOrderDetailsBottonViewDelegate>
@property (nonatomic, strong) YQOrderDetailsHeaderView * headerView;
@property (nonatomic, strong) YQOrderDetailsBottonView * bottonView;

@property (strong, nonatomic) YQPOrderInfoModel *infoModel;

@property (strong, nonatomic) NSArray *orderFeeArray;
@property (strong, nonatomic) NSArray *orderSubFeeArray;

@property (strong, nonatomic) NSArray *orderInfoArray;
@property (strong, nonatomic) NSArray *orderSubInfoArray;

@property (strong, nonatomic) YQPIntegerPayView *integeView;

@end

@implementation YQIntegralOrderDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"订单详情");
    UIImage *rightImage = YQIMG(@"MY_nav_service_icon");
    self.rightItemImage = [rightImage qmui_imageWithTintColor:UIColor.whiteColor];
    self.orderFeeArray = @[kYQLS(@"运费"),kYQLS(@"订单实付款")];
    self.orderInfoArray = @[[NSString stringWithFormat:@"%@ :",kYQLS(@"订单编号")],[NSString stringWithFormat:@"%@:",kYQLS(@"订单创建时间")],[NSString stringWithFormat:@"%@ :",kYQLS(@"支付时间")],[NSString stringWithFormat:@"%@ :",kYQLS(@"发货时间")],[NSString stringWithFormat:@"%@:",kYQLS(@"确认收货时间")]];

    [self getOrderInfoApi];
}
#pragma mark - 获取订单详情
- (void)getOrderInfoApi
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kPOST_ORDER_INFO_URL,self.model.integralOrderId];
    [YQPNetWorkingManager sendPostRequest:url parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (responseObject.code == 0) {
            self.infoModel = [YQPOrderInfoModel mj_objectWithKeyValues:responseObject.data];
            self.headerView.model = self.infoModel;
            self.bottonView.model = self.infoModel;

            self.orderSubFeeArray = @[self.infoModel.freight,[NSString stringWithFormat:@"%@ %@", self.infoModel.totalIntegralAmount,kYQLS(@"积分")]];
            
            NSString *createTime = self.infoModel.createTime.length >0?[YQPInsureValidate timeInStr:self.infoModel.createTime]:kYQLS(@"未创建");
            NSString *paymentTime = self.infoModel.paymentTime.length >0?[YQPInsureValidate timeInStr:self.infoModel.paymentTime]:kYQLS(@"未创建");
            NSString *deliveryTime = self.infoModel.deliveryTime.length >0?[YQPInsureValidate timeInStr:self.infoModel.deliveryTime]:kYQLS(@"未创建");
            NSString *receiveTime = self.infoModel.receiveTime.length >0?[YQPInsureValidate timeInStr:self.infoModel.receiveTime]:kYQLS(@"未创建");
            self.orderSubInfoArray = @[self.infoModel.integralOrderSn,createTime,paymentTime,deliveryTime,receiveTime];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
    }];
}

-(void)setupView {
    [super setupView];
    [self _setupNav];
    UIImage *leftImage = YQIMG(@"nav_back_sIcon");
    [self.leftItem setImage:[leftImage qmui_imageWithTintColor:UIColor.whiteColor] forState:(UIControlStateNormal)];
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(154);
    }];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    self.listTableView.frame = CGRectMake(0, 154, kYQPScreenWidth, kYQPScreenHeight-Height_NavBar-kHeight_Botton_SafeArea-154-49);
    [self.view addSubview:self.bottonView];
    [self.bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderManageGoodsCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderDetailsMoneyCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderDetailsLookLogisticsCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderDetailsInfoCell")
    kYQREGISTERTABLEVIEWCHeaderFooter([YQOrderDetailsTitleHeader class])
}

#pragma -mark 私有
-(void)_setupNav {
    UIFont *font = kSystemMedium(17);
    NSDictionary *dic = @{ NSFontAttributeName:font,NSForegroundColorAttributeName:UIColor.whiteColor};
    self.navigationController.navigationBar.titleTextAttributes =dic;
    // 一行代码搞定导航栏颜色
    [self wr_setNavBarBarTintColor:kAPPThemeColor];
    // 一行代码搞定导航栏底部分割线是否隐藏
    [self wr_setNavBarShadowImageHidden:YES];
    [self wr_setNavBarTitleColor:[UIColor whiteColor]];
    [self wr_setNavBarTintColor:UIColor.whiteColor];
}

-(void)rightItemAction:(UIButton *)sender {
    
}

#pragma makr -- lazy
-(YQOrderDetailsHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [YQOrderDetailsHeaderView viewFromXib];
        _headerView.topBgView.backgroundColor = kAPPThemeColor;
//        _headerView.stateLb.text = @"确认收货";
//        _headerView.timeLb.text = @"剩余: 05小时57小时";
//        _headerView.nameLb.text = @"李晓冬";
//        _headerView.phoneLb.text = @"17727956732";
//        _headerView.locationLb.text = @"地址 : 广东省深圳市南山区蛇口街道创业一号C栋";
    }
    return _headerView;
}

-(YQOrderDetailsBottonView *)bottonView {
    if (!_bottonView) {
        _bottonView = [YQOrderDetailsBottonView viewFromXib];
        _bottonView.backgroundColor = UIColor.whiteColor;
        //2.设置阴影偏移范围
        _bottonView.layer.shadowOffset = CGSizeMake(0, 2);
        //3.设置阴影颜色的透明度
        _bottonView.layer.shadowOpacity = 0.5;
        
        //4.设置阴影半径
        _bottonView.layer.shadowRadius = 4;
        _bottonView.delegate = self;
        //5.设置阴影路径
        _bottonView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.navigationController.navigationBar.bounds].CGPath;
//        [_bottonView.rightBtn setTitle:@"评价" forState:(UIControlStateNormal)];
        ws(self);
//        [_bottonView.rightBtn addTapAction:^(id  _Nonnull sender) {
//            OrderEvaViewController *VC = [[OrderEvaViewController alloc] init];
//            [weakself.navigationController pushViewController:VC animated:true];
//        }];

        if ([self.infoModel.orderStatus isEqualToString:@"3"]) {
            [_bottonView.leftBtn addTapAction:^(id  _Nonnull sender) {
//                [YQOrderRefundShowView refundShowViewViewResultBlack:^(NSInteger row) {
//                    YQOrderRefundVC *refundVC = [[YQOrderRefundVC alloc] init];
//                    refundVC.type = row;
//                    [weakself.navigationController pushViewController:refundVC animated:true];
//                }];
//                return ;
//                YQOrderLogisticsWriteVC *writeVC = [[YQOrderLogisticsWriteVC alloc] init];
//                [weakself.navigationController pushViewController:writeVC animated:true];
            }];
        }
        
    }
    return _bottonView;
}

#pragma makr -- lazy end
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 1){
        return self.orderFeeArray.count;
    }else if (section == 3){
        return 5;
    }
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YQOrderManageGoodsCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderManageGoodsCell"];
        goodsCell.titleLb.text = self.infoModel.goodsName;
        goodsCell.priceLb.text = [NSString stringWithFormat:@"$%.2f",self.infoModel.payIntegralAmount.doubleValue];
        goodsCell.countLb.text = [NSString stringWithFormat:@"x%@",self.infoModel.goodsNumber];
        goodsCell.parameterLb.text = @"";
        [goodsCell.goodsIcon sd_setImageWithURL:[NSURL URLWithString:self.infoModel.defaultImage] placeholderImage:IMAGE_NAMED(@"")];
        return goodsCell;
    }else if (indexPath.section == 1) {
        YQOrderDetailsMoneyCell *moneyCell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsMoneyCell"];
        moneyCell.labTitle.text = self.orderFeeArray[indexPath.row];
        moneyCell.labSubTitle.text = self.orderSubFeeArray[indexPath.row];

        return moneyCell;
    }else if (indexPath.section == 2) {
        YQOrderDetailsLookLogisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsLookLogisticsCell"];
        return cell;
    }
    
    YQOrderDetailsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderDetailsInfoCell"];
    cell.titleLb.text = self.orderInfoArray[indexPath.row];
    cell.infoLb.text = self.orderSubInfoArray[indexPath.row];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 104;
    }else if (indexPath.section == 1){
        return 30;
    }else if (indexPath.section == 2) {
        return 44;
    }
    return 21;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        UIView *view = [UIView new];
        view.backgroundColor = UIColor.whiteColor;
        return view;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 23.5;
    }
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 3) {
        YQOrderDetailsTitleHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YQOrderDetailsTitleHeader"];
        return header;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if(section == 3) {
        return 39;
    }
    return 0.001;
}

- (YQPIntegerPayView *)integeView
{
    if (!_integeView) {
        _integeView = [YQPIntegerPayView new];
    }
    return _integeView;
}
#pragma mark - YQOrderDetailsDelegate
#pragma mark - 待付款订单
- (void)didClickItemToPayModel:(YQPOrderInfoModel *)model
{
    NSLog(@"待付款");
    self.integeView.labSubTitle.text = [NSString stringWithFormat:@"%@ %@",model.totalIntegralAmount,kYQLS(@"积分")];;
    
    [self.integeView show];
    MJWeakSelf
    [self.integeView setSureBlock:^{
        [weakSelf.integeView dismissView];
        [weakSelf payOrderByIntegerApi:model.integralOrderId];

    }];
}

#pragma mark - 积分支付
- (void)payOrderByIntegerApi:(NSString *)integralOrderId
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kPOST_INTEGER_PAY_URL,integralOrderId];
    [YQPNetWorkingManager sendPostRequest:url parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        
        [MBProgressHUD showMessage:kYQLS(@"支付成功")];
        
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}

#pragma mark - 确认收货订单
- (void)didClickItemToConfirmReceiveModel:(YQPOrderInfoModel *)model
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kPOST_INTEGER_RECEIVE_URL,model.integralOrderId];
    [YQPNetWorkingManager sendPostRequest:url parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [MBProgressHUD showMessage:kYQLS(@"收货成功")];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}
#pragma mark - 重新下单
- (void)didClickItemToOrderModel:(YQPOrderInfoModel *)model
{
    
}
#pragma mark - 评价订单
- (void)didClickItemToCommentModel:(YQPOrderInfoModel *)model
{
    OrderEvaViewController *VC = [[OrderEvaViewController alloc] init];
    [self.navigationController pushViewController:VC animated:true];
}
#pragma mark - 取消订单
- (void)didClickItemToCancelModel:(YQPOrderInfoModel *)model
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kPOST_CANCEL_ORDER_URL,model.integralOrderId];
    [YQPNetWorkingManager sendPostRequest:url parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [MBProgressHUD showMessage:kYQLS(@"订单已取消")];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}
#pragma mark - 删除订单
- (void)didClickItemToRemoveModel:(YQPOrderInfoModel *)model
{
    NSString *url = [NSString    stringWithFormat:@"%@%@",kPOST_DELETE_ORDER_URL,model.integralOrderId];
    [YQPNetWorkingManager sendPostRequest:url parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
         [MBProgressHUD showMessage:kYQLS(@"删除成功")];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}
@end
